﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Entity
{
    class TimeCountBar : BaseEntity
    {
        List<AnimationSprite> _iconList = new List<AnimationSprite>();
        AnimationSprite counterSprite, counterSprite10;
        int _timer, _gameTime, _point;
        public delegate void OnGetThreePoint();
        public OnGetThreePoint OnGetThreePointEvent;

        public delegate void OnTimesUp();
        public OnTimesUp OnTimesUpEvent;

        public bool IsStop { get; set; }
        

        public TimeCountBar(Vector2 pos,int timer,string whichEle)
        {
            ZIndex = Constants.ui;
            Position = pos;
            _timer = timer;
            Sprite whiteBackGround = new Sprite();
            whiteBackGround.textureName = "white_draw";
            whiteBackGround.OnLoad();
            whiteBackGround.Position = new Vector2(this.Position.X + 0, this.Position.Y + 0);
            this.Attach(whiteBackGround);

            counterSprite = new AnimationSprite();
            counterSprite.textureName = "number";
            counterSprite.OnLoad();
            counterSprite.SetAnimation(new Point(3, 4));
            counterSprite.Position = new Vector2(Position.X + 80, Position.Y + 15);
            this.Attach(counterSprite);

            counterSprite10 = new AnimationSprite();
            counterSprite10.textureName = "number";
            counterSprite10.OnLoad();
            counterSprite10.SetAnimation(new Point(3, 4));
            counterSprite10.Position = new Vector2(Position.X + 20, Position.Y + 15);
            this.Attach(counterSprite10);

            Sprite timeSecond = new Sprite();
            timeSecond.textureName = "second";
            timeSecond.OnLoad();
            timeSecond.Position = new Vector2(Position.X + 145, Position.Y + 50);
            this.Attach(timeSecond);

            for (int i = 0; i < 3; i++)
            {
                AnimationSprite icon = new AnimationSprite();
                icon.textureName = whichEle;
                icon.OnLoad();
                icon.SetAnimation(new Point(1, 2),new int[] {0});
                icon.Position = new Vector2(Position.X + 195 + 65*i, Position.Y + 20);
                this.Attach(icon);
                _iconList.Add(icon);
            }
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            _gameTime += delta;
            if (_gameTime / 1000 >= 1)
            {
                if (!IsStop) _timer--;
                _gameTime = _gameTime % 1000;
                //GetPoint();//test
            }
            Convert(_timer);
            if (_timer == 0 && OnTimesUpEvent != null) 
            {
                OnTimesUpEvent();
            }
        }

        public void GetPoint() 
        {
            if(_point <= 2)_iconList[_point].SetChangeFrameAnimation(new int[] { 1 });
            _point++;
            if (_point == 3 && OnGetThreePointEvent != null) 
            {
                OnGetThreePointEvent();
            }
        }

        void Convert(int i)
        {
            if (i < 0) i = 0;
            counterSprite10.SetChangeFrameAnimation(new int[] { i / 10 });
            counterSprite.SetChangeFrameAnimation(new int[] { i % 10 });
        }
    }
}
